package com.techshine.common.page;

import java.io.BufferedReader;
import java.io.Reader;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.druid.proxy.jdbc.ClobProxyImpl;

/**
 * 
 *
 * @Description 
 * @author (作者) liukd
 * @date (开发日期) 2016年10月27日 下午3:06:58 
 * @company (开发公司) 北京精仪达盛科技有限公司
 * @copyright (版权) 本文件归北京精仪达盛科技有限公司所有
 * @version (版本) V1.0
 * @since (该版本支持的JDK版本) ： 1.7
 * @modify (修改) 
 *                  
 *                  
 *                  
 * @Review (审核人)
 */
@SuppressWarnings("rawtypes")
public class PageData extends HashMap implements Map {
    
    private static final long serialVersionUID = 1L;
    
    Map<Object, Object> map = null;
    
    HttpServletRequest request;
    
    public PageData(HttpServletRequest request) {
        this.request = request;
        Map<?, ?> properties = request.getParameterMap();
        Map<Object, Object> returnMap = new HashMap<Object, Object>();
        Iterator<?> entries = properties.entrySet().iterator();
        Map.Entry entry;
        String name = "";
        String value = "";
        while (entries.hasNext()) {
            entry = (Map.Entry)entries.next();
            name = (String)entry.getKey();
            Object valueObj = entry.getValue();
            if (null == valueObj) {
                value = "";
            }
            else if (valueObj instanceof String[]) {
                String[] values = (String[])valueObj;
                for (int i = 0; i < values.length; i++) {
                    value = values[i] + ",";
                }
                value = value.substring(0, value.length() - 1);
            }
            else {
                value = valueObj.toString();
            }
            returnMap.put(name, value);
        }
        map = returnMap;
    }
    
    public PageData() {
        map = new HashMap<Object, Object>();
    }
    
    @Override
    public Object get(Object key) {
        Object obj = null;
        if (map.get(key) instanceof Object[]) {
            Object[] arr = (Object[])map.get(key);
            obj = request == null ? arr : (request.getParameter((String)key) == null ? arr : arr[0]);
        }
        else {
            obj = map.get(key);
        }
        return obj;
    }
    
    public String getString(Object key) {
        return (String)get(key);
    }
    
    @Override
    public Object put(Object key, Object value) {
        if (value instanceof ClobProxyImpl) {
            try {
                ClobProxyImpl clob = (ClobProxyImpl)value;
                Reader is = clob.getCharacterStream();// 得到流
                BufferedReader br = new BufferedReader(is);
                String s = br.readLine();
                StringBuffer sb = new StringBuffer();
                while (s != null) {// 执行循环将字符串全部取出付值给StringBuffer由StringBuffer转成STRING
                    sb.append(s);
                    s = br.readLine();
                }
                value = sb.toString();
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
        return map.put(key, value);
    }
    
    @Override
    public Object remove(Object key) {
        return map.remove(key);
    }
    
    public void clear() {
        map.clear();
    }
    
    public boolean containsKey(Object key) {
        // TODO Auto-generated method stub
        return map.containsKey(key);
    }
    
    public boolean containsValue(Object value) {
        // TODO Auto-generated method stub
        return map.containsValue(value);
    }
    
    public Set entrySet() {
        // TODO Auto-generated method stub
        return map.entrySet();
    }
    
    public boolean isEmpty() {
        // TODO Auto-generated method stub
        return map.isEmpty();
    }
    
    public Set keySet() {
        // TODO Auto-generated method stub
        return map.keySet();
    }
    
    @SuppressWarnings("unchecked")
    public void putAll(Map t) {
        // TODO Auto-generated method stub
        map.putAll(t);
    }
    
    public int size() {
        // TODO Auto-generated method stub
        return map.size();
    }
    
    public Collection values() {
        // TODO Auto-generated method stub
        return map.values();
    }
    
}
